

/*
plot model estimates and responses

output:
- f5b_*: model figures

*/


* plotting options
set scheme lean_uncluttered



********************************************************************************
********************************************************************************
* compare model/data fit
n di "plotting model/data comparisons"

**************************************************
* baseline localization coefficients

use ../data/rolling/phreg_baseline.dta, clear
gen tau = maturity_end - 1
merge 1:1 tau using ../data/model/localization_baseline.dta, nogen
drop if tau<=0
* add tau=0 observation
set obs `=_N+1'
replace tau = 0 if missing(tau)
sort tau

local reg_types_data S0 L0 S1 L1
local reg_types_model short_low long_low short_high long_high

forval ii=1/4 {
	local r_data: word `ii' of `reg_types_data'
	local r_model: word `ii' of `reg_types_model'
	* create confidence intervals
	local coeff_data D_star_`r_data'
	gen cil_`coeff_data' = `coeff_data' - 2*sqrt(V_`coeff_data'_`coeff_data')
	gen ciu_`coeff_data' = `coeff_data' + 2*sqrt(V_`coeff_data'_`coeff_data')
	* fix tau=0
	replace `coeff_data' = 0 if tau==0
	replace ciu_`coeff_data' = 0 if tau==0
	replace cil_`coeff_data' = 0 if tau==0
	replace b_`r_model' = 0 if tau==0
	
	* scaling and labeling for axes
	if `ii'==1 {
		local legend_cmd legend(order(1 "Data" 4 "Model") ///
			ring(0) position(1) rows(2) ///
			size(small) rowgap(0) colgap(0) )
	}
	else {
		local legend_cmd legend(off)
	}
	twoway ///
		(line `coeff_data' ciu_`coeff_data' cil_`coeff_data' tau, ///
			lcolor("scheme p1" "scheme p1" "scheme p1") ///
			lwidth(medium thin thin) ///
			lpattern(solid shortdash shortdash ) ) ///
		(line b_`r_model' tau, ///
			lcolor("scheme p2") ///
			lwidth(medium) lpattern(solid ) ) ///
		, yscale(r(-0.25 2)) ylabel(0[0.5]2) ///
		`legend_cmd' ///
		ytitle("Coefficient") xtitle("") title("") ///
		name(ph_`r_model'_compare, replace) nodraw

}



**************************************************
* maturity moments

use ../data/model/target_moments.dta, clear

local moment_types D12_y D1_y D12_y_D12_ishort_0 D1_y_D1_ishort_0
forval ii=1/4 {
	local moment_type: word `ii' of `moment_types'
	
	* lengend on first plot only
	if `ii'==1 {
		local legend_cmd legend(order(1 "Data" 2 "Model") ///
			ring(0) position(1) rows(2) ///
			size(small) rowgap(0) colgap(0) )
	}
	else {
		local legend_cmd legend(off)
	}
	* scaling and labeling for axes
	if `ii'==1 {
		local yaxis_cmd yscale(r(0. 2)) ylabel(0[0.5]2) ytitle("Percent")
		local xaxis_cmd xtitle("")
	}
	else if `ii'==2 {
		local yaxis_cmd yscale(r(0. 0.75)) ylabel(0[0.25]0.75) ytitle("Percent")
		local xaxis_cmd xtitle("")
	}
	else if `ii'==3 {
		local yaxis_cmd yscale(r(0. 1)) ylabel(0[0.5]1) ytitle("Correlation")
		local xaxis_cmd  xscale(titlegap(-3) outergap(-3)) xtitle("Maturity")
	}
	else if `ii'==4 {
		local yaxis_cmd yscale(r(0. 1)) ylabel(0[0.5]1) ytitle("Correlation")
		local xaxis_cmd  xscale(titlegap(-3) outergap(-3)) xtitle("Maturity")
	}
	
	twoway ///
		(scatter bhat_`moment_type' tau, lcolor("scheme p1") m(Oh) ) ///
		(line b_`moment_type' tau, lcolor("scheme p2") lpattern(solid) ) ///
		,  `yaxis_cmd' `xaxis_cmd' `legend_cmd' title("") ///
		name("`moment_type'", replace) nodraw

}


* combine all figures
local fname f5b_mat_moments
* rescale *0.8 for large combined figure
graph combine ///
	ph_short_low_compare ph_long_low_compare ///
	ph_short_high_compare ph_long_high_compare ///
	D12_y D1_y ///
	D12_y_D12_ishort_0 D1_y_D1_ishort_0 , ///
	rows(4) imargin(tiny) xsize(14) ysize(18) name(`fname', replace) iscale(*0.8)
* add titles
gr_edit .plotregion1.graph1.title.text.Arrpush "Panel A: Short Non-Crisis"
gr_edit .plotregion1.graph2.title.text.Arrpush "Panel B: Long Non-Crisis"
gr_edit .plotregion1.graph3.title.text.Arrpush "Panel C: Short Crisis"
gr_edit .plotregion1.graph4.title.text.Arrpush "Panel D: Long Crisis"

gr_edit .plotregion1.graph5.title.text.Arrpush "Panel E: {&sigma}({&Delta}y{sub:t}{sup:({&tau})}) "
gr_edit .plotregion1.graph6.title.text.Arrpush "Panel F: {&sigma}({&Delta}{sub:s}y{sub:t}{sup:({&tau})})"
gr_edit .plotregion1.graph7.title.text.Arrpush "Panel G: {&rho}({&Delta}y{sub:t}{sup:({&tau})}, {&Delta}y{sub:t}{sup:(1)}) "
gr_edit .plotregion1.graph8.title.text.Arrpush "Panel H: {&rho}({&Delta}{sub:s}y{sub:t}{sup:({&tau})}, {&Delta}{sub:s}y{sub:t}{sup:(1)})"

graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace




********************************************************************************
********************************************************************************
* alt localization
n di "plotting alt localization coefficients"

use ../data/rolling/phaltreg_D_b2c_all.dta, clear
gen tau = maturity_end - 1
merge 1:1 tau using ../data/model/alt_localization.dta, nogen
drop if tau<=0
* add tau=0 observation
set obs `=_N+1'
replace tau = 0 if missing(tau)
sort tau

local reg_types_data S0 L0 S1 L1
local reg_types_model short_low long_low short_high long_high

forval ii=1/4 {
	local r_data: word `ii' of `reg_types_data'
	local r_model: word `ii' of `reg_types_model'
	
	* create confidence intervals
	local coeff_data B_shock_`r_data'
	gen cil_`coeff_data' = `coeff_data' - 2*sqrt(V_`coeff_data'_`coeff_data')
	gen ciu_`coeff_data' = `coeff_data' + 2*sqrt(V_`coeff_data'_`coeff_data')
	* fix tau=0
	replace `coeff_data' = 0 if tau==0
	replace ciu_`coeff_data' = 0 if tau==0
	replace cil_`coeff_data' = 0 if tau==0
	replace b_`r_model' = 0 if tau==0

	* lengend/axes options
	if `ii'==1 {
		local legend_cmd legend(order(1 "Data" 4 "Model") ///
			ring(0) position(1) rows(2) ///
			size(small) rowgap(0) colgap(0) )
	}
	else {
		local legend_cmd legend(off)
	}
	if inlist(`ii', 3, 4) {
		local xaxis_cmd xtitle("Maturity")
	}
	else {
		local xaxis_cmd xtitle("")
	}
	twoway ///
		(line `coeff_data' ciu_`coeff_data' cil_`coeff_data' tau, ///
			lcolor("scheme p1" "scheme p1" "scheme p1") ///
			lwidth(medium thin thin) ///
			lpattern(solid shortdash shortdash ) ) ///
		(line b_`r_model' tau, ///
			lcolor("scheme p2") ///
			lwidth(medium) lpattern(solid ) ) ///
		, yscale(r(-0.25 6)) ylabel(0[2]6) ///
		`legend_cmd' ///
		ytitle("Coefficient") `xaxis_cmd' title("") ///
		name(alt_ph_`r_model'_compare, replace) nodraw

}

* combine with correct size and export
local fname f5b_alt_ph_reg_coeffs
* vertical
graph combine alt_ph_short_low_compare alt_ph_long_low_compare ///
	alt_ph_short_high_compare alt_ph_long_high_compare, ///
	rows(2) imargin(tiny) xsize(8) ysize(5) name(`fname', replace)
gr_edit .plotregion1.graph1.title.text.Arrpush "Panel A: Short Non-Crisis"
gr_edit .plotregion1.graph2.title.text.Arrpush "Panel B: Long Non-Crisis"
gr_edit .plotregion1.graph3.title.text.Arrpush "Panel C: Short Crisis"
gr_edit .plotregion1.graph4.title.text.Arrpush "Panel D: Long Crisis"

graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace





********************************************************************************
********************************************************************************
* risky localization
n di "plotting risky localization coefficients"

use ../data/model/risky_localization.dta, clear

* two relative localization results
* across maturities (rel1) and spreads (reldiff)
foreach shock_type in short_low short_high long_low long_high {
	* rel1: relative to safe yiled, tau=3 as in baseline
	gen y_til_rel1_`shock_type' = y_til_`shock_type' / y_`shock_type'[31]
	
	* rel2: relative to safe yield, entire maturity
	gen y_til_rel2_`shock_type' = y_til_`shock_type' / y_`shock_type'
	*gen y_til_rel2_`shock_type' = y_til_`shock_type' - y_`shock_type'
}
* reldiff: high vs low
gen y_til_reldiff_short = y_til_rel2_short_high / y_til_rel2_short_low
gen y_til_reldiff_long = y_til_rel2_long_high / y_til_rel2_long_low


* low/high for relative localization
twoway line y_til_rel1_short_low y_til_rel1_long_low tau, ///
	legend(order(1 "Short Shock" 2 "Long Shock") ///
		ring(0) position(1) rows(2) ///
	) ///
	yscale(range(0, 1.5)) ylabel(0(0.5)1.5) ///
	ytitle("Coefficient") xtitle("") title("") ///
	name("ph_risky_rel1_low", replace) nodraw

twoway line y_til_rel1_short_high y_til_rel1_long_high tau, ///
	legend( off ) ytitle("Coefficient") xtitle("") title("") ///
	yscale(range(0, 1.5)) ylabel(0(0.5)1.5) ///
	name("ph_risky_rel1_high", replace) nodraw

twoway line y_til_reldiff_short y_til_reldiff_long tau if tau>=1, ///
	legend( off ) /// yscale( titlegap(-10) ) ///
	yscale(range(0.8, 1)) ylabel(0.8(0.1)1) ///
	ytitle("Coefficient") xtitle("Maturity") title("") ///
	name("ph_risky_reldiff", replace) nodraw

* combine with correct size and export
local fname f5b_risky_ph_reg_coeffs
* vertical
graph combine ph_risky_rel1_low ph_risky_rel1_high ph_risky_reldiff, ///
	rows(3) imargin(tiny) xsize(6) ysize(10) name(`fname', replace)
gr_edit .plotregion1.graph1.title.text.Arrpush "Panel A: Risky Localization (Non-Crisis)"
gr_edit .plotregion1.graph2.title.text.Arrpush "Panel B: Risky Localization (Crisis)"
gr_edit .plotregion1.graph3.title.text.Arrpush "Panel C: Spread Localization"

graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace






